API - Project

U.S. Bureau of Labor Statistics

Author

David Pineda

Introduction

This project utilizes data provided by the U.S. Bureau of Labor Statistics (BLS) to analyze employment trends across various states in the U.S. The data includes detailed occupational employment and wage estimates which are essential for understanding labor market dynamics.

Data Source

The data for this analysis is sourced from the U.S. Bureau of Labor Statistics through their public API. Detailed information about how to access this data and the structure of the API can be found here.

These estimates are calculated with data collected from employers in all industry sectors, all metropolitan and nonmetropolitan areas, and all states and the District of Columbia. The top employment and wage figures are provided above. The complete list is available in the downloadable XLS files.

The percentile wage estimate is the value of a wage below which a certain percent of workers fall. The median wage is the 50th percentile wage estimate—50 percent of workers earn less than the median and 50 percent of workers earn more than the median. More about percentile wages.

  1. Estimates for detailed occupations do not sum to the totals because the totals include occupations not shown separately. Estimates do not include self-employed workers.

  2. Annual wages have been calculated by multiplying the hourly mean wage by a “year-round, full-time” hours figure of 2,080 hours; for those occupations where there is not an hourly wage published, the annual wage has been directly calculated from the reported survey data.

  3. The relative standard error (RSE) is a measure of the reliability of a survey statistic. The smaller the relative standard error, the more precise the estimate.

  4. Estimate not released.

  5. The location quotient is the ratio of the area concentration of occupational employment to the national average concentration. A location quotient greater than one indicates the occupation has a higher share of employment than average, and a location quotient less than one indicates the occupation is less prevalent in the area than average.

Example pull API

Show the code
library(httr)
library(tidyverse)
library(glue)
library(tibble)
library(dplyr)
library(plotly)
library(pander)
Show the code
api_key = '56b0c7cf9f8d4a14a7450c82ad433294'
url = 'https://api.bls.gov/publicAPI/v2/timeseries/data/'

Data Acquisition and Preparation

Function to Fetch Data

This function is designed to retrieve data based on a series of IDs and state names:

Show the code
# Function to get data based on seriesid and state name
get_state_data <- function(seriesid_list, state_name, api_key, url) {
  # Prepare the payload
  payload <- list(
    seriesid = seriesid_list,
    startyear = "2023",
    endyear = "2023",
    registrationkey = api_key
  )
  
  # Make the POST request
  response <- POST(url = url,
                   body = payload,
                   content_type("application/json"),
                   encode = "json")
  
  # Parse the response
  list_obj <- content(response, "text") %>% 
    jsonlite::fromJSON()
  
  # Convert response to dataframe
  df <- map_dfr(list_obj$Results$series$data, ~ tibble(
    year = .x[[1]],
    period = .x[[2]],
    period_type = .x[[3]],
    is_annual = .x[[4]],
    value = .x[[5]]
  ))
  
  # Define column names
  column_names <- c("Employment (1)", 
                    "Employment per thousand jobs", 
                    "Location quotient (9)", 
                    "Hourly mean wage", 
                    "Annual mean wage (2)")
  
  # Mutate dataframe to include series titles
  df <- df %>%
    mutate(series_title = column_names)
  
  # Pivot dataframe and add state information
  state_df <- df %>% 
    pivot_wider(
      names_from = series_title,
      values_from = value
    ) %>% 
    mutate(State = state_name) %>% 
    select(State, `Employment (1)`, `Employment per thousand jobs`, `Location quotient (9)`, `Hourly mean wage`, `Annual mean wage (2)`)
  
  return(state_df)
}

Texas

Show the code
seriesid_list <- c("OEUS480000000000015205101", "OEUS480000000000015205116", "OEUS480000000000015205117", "OEUS480000000000015205103", "OEUS480000000000015205104")
state_name <- "Texas"
texas_df <- get_state_data(seriesid_list, state_name, api_key, url)

Utah

Show the code
seriesid_list <- c("OEUS490000000000015205101", "OEUS490000000000015205116", "OEUS490000000000015205117", "OEUS490000000000015205103", "OEUS490000000000015205104")
state_name <- "Utah"
utah_df <- get_state_data(seriesid_list, state_name, api_key, url)

New York

Show the code
seriesid_list <- c("OEUS360000000000015205101", "OEUS360000000000015205116", "OEUS360000000000015205117", "OEUS360000000000015205103", "OEUS360000000000015205104")
state_name <- "New_York"
ny_df <- get_state_data(seriesid_list, state_name, api_key, url)

Florida

Show the code
seriesid_list <- c("OEUS120000000000015205101", "OEUS120000000000015205116", "OEUS120000000000015205117", "OEUS120000000000015205103", "OEUS120000000000015205104")
state_name <- "Florida"
fl_df <- get_state_data(seriesid_list, state_name, api_key, url)

Pennsylvania

Show the code
seriesid_list <- c("OEUS420000000000015205101", "OEUS420000000000015205116", "OEUS420000000000015205117", "OEUS420000000000015205103", "OEUS420000000000015205104")
state_name <- "Pennsylvania"
pnsl_df <- get_state_data(seriesid_list, state_name, api_key, url)

California

Show the code
seriesid_list <- c("OEUS060000000000015205101", "OEUS060000000000015205116", "OEUS060000000000015205117", "OEUS060000000000015205103", "OEUS060000000000015205104")
state_name <- "California"
cali_df <- get_state_data(seriesid_list, state_name, api_key, url)

Combining Tables

Show the code
df_table <- bind_rows(cali_df, texas_df, ny_df, fl_df, pnsl_df, utah_df)
pander(df_table)
Table continues below
State Employment (1) Employment per thousand jobs
California 33220 1.851
Texas 20560 1.517
New_York 16390 1.745
Florida 8400 0.878
Pennsylvania 7490 1.259
Utah 3530 2.105
Location quotient (9) Hourly mean wage Annual mean wage (2)
1.46 67.54 140490
1.20 52.40 109000
1.38 64.82 134830
0.69 51.20 106490
0.99 49.22 102370
1.66 48.93 101780
Show the code
df_long <- df_table %>%
  pivot_longer(cols = -State, names_to = "Metric", values_to = "Value")

Analysis and Visualization

Conclusion

This analysis provided insights into employment patterns across different states in the U.S., highlighting variations in employment rates, wage levels, and occupational distribution. Understanding these trends is vital for policymakers, economists, and the general public to make informed decisions about employment and economic strategies.